﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using WebTemplate.DAL.Repositories;
using WebTemplate.DAL.Entities;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Net.Mail;
using System.Net;

namespace WebTemplate.ControlPanel.Account
{
    public partial class SemesterDetails : System.Web.UI.Page
    {

        private int? ID
        {
            get
            {
                var idstr = this.Request.QueryString["id"];
                if (string.IsNullOrWhiteSpace(idstr))
                    return null;
                return int.Parse(idstr);
            }
        }

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {

                CoursesRepository repository = new CoursesRepository();
                DoctorRepository Repository = new DoctorRepository();
                DoctorInCoursesReporsitory RepositorY = new DoctorInCoursesReporsitory();

                Course.DataSource = repository.DropDownList();
                Course.DataValueField = "Name";
                Course.DataTextField = "Name";
                Course.DataBind();

                Doctor.DataSource = Repository.DropDownList();
                Doctor.DataValueField = "Name";
                Doctor.DataTextField = "Name";
                Doctor.DataBind();

                Location.DataSource = RepositorY.DropDownList();
                Location.DataValueField = "LectureTime";
                Location.DataTextField = "LectureTime";
                Location.DataBind();


                FillResult();
            }
        }

        

        protected void CurrentCourses_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.PageIndex = e.NewPageIndex;
            FillResult();
        }

        protected void CurrentCourses_Sorting(object sender, GridViewSortEventArgs e)
        {
            this.SortExpression = e.SortExpression;
            this.PageIndex = 0;
            FillResult();
        }

        protected void CurrentCourses_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteCourse")
            {
                int rowindex = int.Parse(e.CommandArgument.ToString());
                int Courseid = (int)CurrentCourses.DataKeys[rowindex].Value;
                CoursesRepository repositroy = new CoursesRepository();
                repositroy.Delete(Courseid);
                FillResult();
            }
        }
        
        #endregion
        
       
        
        #region Events

        private CoursesSearchCriteria SearchCriteria
        {
            get
            {
                return this.ViewState["SearchCriteria"] as CoursesSearchCriteria;
            }

            set
            {
                this.ViewState["SearchCriteria"] = value;
            }
        }
        //private DoctorSearchCriteria searchcriteria
        //{
        //    get
        //    {
        //        return this.ViewState["searchcriteria"] as DoctorSearchCriteria;
        //    }
        //    set
        //    {
        //        this.ViewState["searchcriteria"] = value;
        //    }
        //}

        //private DoctorInCoursesSearchCriteria Searchcriteria
        //{
        //    get
        //    {
        //        return this.ViewState["Searchcriteria"] as DoctorInCoursesSearchCriteria;
        //    }
        //    set
        //    {
        //        this.ViewState["Searchcriteria"] = value;
        //    }
        //}

        private string SortExpression
        {
            set
            {
                this.ViewState["SortExpression"] = value;
            }
            get
            {
                return this.ViewState["SortExpression"] as string;
            }
        }

        private int PageIndex
        {
            set
            {
                this.ViewState["PageIndex"] = value;
            }
            get
            {
                return this.ViewState["PageIndex"] == null ? 0 : (int)this.ViewState["PageIndex"];
            }
        }

        private void FillResult()
        {
            var searchCriteria = this.SearchCriteria;
            //var search = this.searchcriteria;
            //var criteria = this.Searchcriteria;
            var sortExpression = this.SortExpression;

            if (searchCriteria == null)
            {
                searchCriteria = new CoursesSearchCriteria();
                this.SearchCriteria = searchCriteria;
            }

            //if (search == null)
            //{
            //    search = new DoctorSearchCriteria();
            //    this.searchcriteria = search;
            //}

            //if(criteria == null)
            //{
            //    criteria = new DoctorInCoursesSearchCriteria();
            //    this.Searchcriteria = criteria;
            //}

            if (string.IsNullOrWhiteSpace(sortExpression))
            {
                sortExpression = this.SortExpression = "Id";
            }
            CoursesRepository reprositry = new CoursesRepository();
            //DoctorRepository Repository = new DoctorRepository();
            //DoctorInCoursesReporsitory RepositorY = new DoctorInCoursesReporsitory();
            IEnumerable<Courses> result = reprositry.Select(searchCriteria);
            //IEnumerable<Doctor> one = Repository.Select(search);
            //IEnumerable<DoctorInCourses> two = RepositorY.Select(criteria); 


            if (sortExpression == "Name")
                result = result.OrderBy(x => x.Name).ToList();
            //else if (sortExpression == "Doctor")
            //    one = one.OrderBy(x => x.Name).ToList();
            //else if (sortExpression == "Time")
            //    two = two.OrderBy(x => x.LectureTime).ToList();
            //else if (sortExpression == "Location")
            //    two = two.OrderBy(x => x.LecturePlace).ToList();
            CurrentCourses.PageIndex = this.PageIndex;
            CurrentCourses.DataSource = result.ToList();
            //CurrentCourses.DataSource = one.ToList();
            //CurrentCourses.DataSource = two.ToList();
            CurrentCourses.DataBind();
        }

        #endregion

        protected void saving_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                CoursesRepository reprositry = new CoursesRepository();
                Courses course;
                //DoctorRepository Repository = new DoctorRepository();
                //Doctor doctor;
                //DoctorInCoursesReporsitory RepositorY = new DoctorInCoursesReporsitory();
                //DoctorInCourses doctorincourse;

                if (ID.HasValue)
                {
                    course = reprositry.Select(new CoursesSearchCriteria() { ID = this.ID.Value }).First();
                    //doctor = Repository.Select(new DoctorSearchCriteria() { ID = this.ID.Value }).First();
                    //doctorincourse = RepositorY.Select(new DoctorInCoursesSearchCriteria() { ID = this.ID.Value }).First();
                }
                else
                {
                    course = new Courses();
                    //doctor = new Doctor();
                    //doctorincourse = new DoctorInCourses();
                }

                course.Name = Course.Text;
                //doctor.Name = Doctor.Text;
                //doctorincourse.LectureTime = Convert.ToDateTime(Time.Text);
                //doctorincourse.LecturePlace = Location.Text;
            }

            Response.Redirect("~/Account/SemesterDetails.aspx");
        }

    }

        
}